const bt = require('./bt');

const postorder = (root) => {
	if (!root) return
	postorder(root.left)
	postorder(root.right)
	console.log(root.val)
};

const postorderNoRe = (root) => {
	if(!root) return
	const stack = [root]
	const outputStack = []
	while(stack.length){
		const top = stack.pop()
		top.left && stack.push(top.left)
		top.right && stack.push(top.right)
		outputStack.push(top)
	}

	while(outputStack.length){
		const top = outputStack.pop()
		console.log(top.val)
	}
};

// const postorder = (root) => {
// 	if (!root) { return; }
// 	postorder(root.left);
// 	postorder(root.right);
// 	console.log(root.val);
// };

// const postorder = (root) => {
//     if (!root) { return; }
//     const outputStack = [];
//     const stack = [root];
//     while (stack.length) {
//         const n = stack.pop();
//         outputStack.push(n);
//         if (n.left) stack.push(n.left);
//         if (n.right) stack.push(n.right);
//     }
//     while(outputStack.length){
//         const n = outputStack.pop();
//         console.log(n.val);
//     }
// };

postorder(bt);
console.log('------------------')
postorderNoRe(bt);
